
*****************************************************************************
*********            Table 2 - Hedonic Regressions I-10 West
*****************************************************************************
clear
set seed 1234
local cutoff=0.01									// where to truncate reliability (0 or 0.01)
capture log close
capture file close myfile
log using ".\results\revision1\Table2.txt", text replace
putexcel set ".\results\revision1\maintextFigsTabs.xlsx", sheet("Table 2") modify
putexcel B2="I"
putexcel D2="II"
putexcel F2="III"	
putexcel H2="IV"
putexcel J2="V"
putexcel B3 ="Full Sample"
putexcel D3 =">5 appearances"
putexcel F3 = "Full Sample"
putexcel H3 ="After Oct. 20th, 2013"
putexcel J3 ="Split Sample IV"
putexcel A4 = "Constant"
putexcel A8 = "Travel Time"
putexcel A12 = "Reliability"
putexcel A16 = "Heterogeneous Agent Model"
putexcel F16 = "X"
putexcel H16 = "X"
putexcel J16 = "X"
putexcel A17 = "Mean Toll Paid"
putexcel A18 = "Urgency's Share of WTP"
putexcel A19 = "Mean Time Savings in Minutes"
putexcel A20 = "Mean Reliability in Minutes"
putexcel A21 = "Number of Accounts"


*******************************************************************************
***			 Column I --- Full Sample Homog. Agent 					*******
*******************************************************************************

local cutoff=0.01
clear
save ".\data\temp\temp1", replace emptyok

* Get reliability
*I 10 W
use ".\data\clean\HV_ML_reliab", clear
keep if fwy==10&dir=="W"
format date %d
sort date hour
tempfile temp2
save `temp2'


use ".\data\clean\I10W_laneuse_dataset_15nov14_wcensus", clear

sort date hour
merge m:1 date hour using `temp2', keep(1 3) nogen 
drop if weekend==1
keep if inlist(hour,5,6,7,8)
drop if holiday==1

*WTP calculation
gen EL_TT=dist/ELspeed
gen ML_TT=dist/MLspeed
gen MP=inlist(hour,5,6,7,8)&weekend==0
gen TT_dif_hr=ML_TT-EL_TT
gen WTP2=charged_toll/TT_dif_hr



gen reliabilityML=dist/p20_speedML-dist/p50_speedML
gen reliabilityHV=dist/p20_speedHV-dist/p50_speedHV
gen reliability_diff=reliabilityML-reliabilityHV
replace reliability_diff=0 if reliability_diff<`cutoff'
keep if WTP2>0&WTP2~=.
keep if reliability_diff~=.
drop if TT_dif_hr==.
keep if acct_type=="PRIVATE"&occupancy~="HOV-3"



reg charged_toll TT_dif_hr reliability_diff if TT_dif_hr>0  , cluster(rt_id)
local i=4
local j=`i'+1
foreach v in  _cons TT_dif_hr reliability_diff{
	local b`v' = round(_b[`v'],0.01)
	local b`v':di %7.2f `b`v''
	local se`v' = round(_se[`v'],0.01)
	local se`v':di %7.2f `se`v''
	if inrange(2 * ttail(e(df_r), abs(_b[`v']/_se[`v'])),0.000000000,0.01) local star`v' = "***"
	else if inrange(2 * ttail(e(df_r), abs(_b[`v']/_se[`v'])),0.010000001,0.05) local star`v' = "**"
	else if inrange(2 * ttail(e(df_r), abs(_b[`v']/_se[`v'])),0.050000001,0.10) local star`v' = "*"
	else local star`v' = " "
	qui putexcel B`i' = "`b`v''`star`v''"
	qui putexcel B`j' = "(`se`v'')"
	local i=`i'+4
	local j=`i'+1
	}
local N1A = `e(N)'
foreach v in TT_dif_hr reliability_diff charged_toll{
	qui sum `v' if e(sample)==1
	local m`v'=round(`r(mean)',.01)
	}
putexcel B17="$`mcharged_toll'"
local shr=100*round(`b_cons'/`mcharged_toll',.01)
putexcel B18="`shr'%"
local ttd=round(`mTT_dif_hr'*60,.01)
local ttd:di %7.2f `ttd'
putexcel B19="`ttd'"
local rd=round(`mreliability_diff'*60,.01)
local rd: di %7.2f `rd'
putexcel B20="`rd'"
qui unique acct_n if e(sample)==1
local an=`r(unique)'
putexcel B21=`an'


*******************************************************************************
***			 Column II --- Full Sample Homog. Agent 5 or more appearances *******
******************************************************************************
preserve
use ".\data\temp\acct_by_acct_noMP5_wvor",clear
drop if urgency==0 | urgency==.
drop if VOT==0 | VOT==.
drop if VOR==0 | VOR==.

drop if urgencyse==0 | urgencyse==.
drop if VOTse==0 | VOTse==.
drop if VORse==0 | VORse==.
keep acct_no
tempfile col2
save `col2'
restore
merge m:1 acct_no using `col2', nogen keep(3)
local c "D"

reg charged_toll TT_dif_hr reliability_diff if TT_dif_hr>0  , cluster(rt_id)
local i=4
local j=`i'+1
foreach v in  _cons TT_dif_hr reliability_diff{
	local b`v' = round(_b[`v'],0.01)
	local b`v':di %7.2f `b`v''
	local se`v' = round(_se[`v'],0.01)
	local se`v':di %7.2f `se`v''
	if inrange(2 * ttail(e(df_r), abs(_b[`v']/_se[`v'])),0.000000000,0.01) local star`v' = "***"
	else if inrange(2 * ttail(e(df_r), abs(_b[`v']/_se[`v'])),0.010000001,0.05) local star`v' = "**"
	else if inrange(2 * ttail(e(df_r), abs(_b[`v']/_se[`v'])),0.050000001,0.10) local star`v' = "*"
	else local star`v' = " "
	qui putexcel `c'`i' = "`b`v''`star`v''"
	qui putexcel `c'`j' = "(`se`v'')"
	local i=`i'+4
	local j=`i'+1
	}
local N1A = `e(N)'
foreach v in TT_dif_hr reliability_diff charged_toll{
	qui sum `v' if e(sample)==1
	local m`v'=round(`r(mean)',.01)
	}
putexcel `c'17="$`mcharged_toll'"
local shr=100*round(`b_cons'/`mcharged_toll',.01)
putexcel `c'18="`shr'%"
local ttd=round(`mTT_dif_hr'*60,.01)
local ttd:di %7.2f `ttd'
putexcel `c'19="`ttd'"
local rd=round(`mreliability_diff'*60,.01)
local rd: di %7.2f `rd'
putexcel `c'20="`rd'"
qui unique acct_no if e(sample)==1
local an=`r(unique)'
putexcel `c'21=`an'



*******************************************************************************
***			 Column III --- Full Sample Heterog. Agent 					*******
******************************************************************************
local cutoff=0.01									// where to truncate reliability (0 or 0.01)

local c "F"

clear
save ".\data\temp\temp1", replace emptyok

* Get reliability
*I 10 W
use ".\data\clean\HV_ML_reliab", clear
keep if fwy==10&dir=="W"
format date %d
sort date hour
save ".\data\temp\temp2", replace


use ".\data\clean\I10W_laneuse_dataset_15nov14_wcensus", clear

sort date hour
merge m:1 date hour using ".\data\temp\temp2", keep(1 3) nogen 
drop if weekend==1
keep if inlist(hour,5,6,7,8)
*keep if date>td(20oct2013)
drop if holiday==1
drop if transponder==""
drop if transponder=="."
*WTP calculation
gen EL_TT=dist/ELspeed
gen ML_TT=dist/MLspeed
gen MP=inlist(hour,5,6,7,8)&weekend==0
gen TT_dif_hr=ML_TT-EL_TT
gen WTP2=charged_toll/TT_dif_hr



gen reliabilityML=dist/p20_speedML-dist/p50_speedML
gen reliabilityHV=dist/p20_speedHV-dist/p50_speedHV
gen reliability_diff=reliabilityML-reliabilityHV
replace reliability_diff=0 if reliability_diff<`cutoff'

keep if WTP2>0&WTP2~=.
keep if reliability_diff~=.
drop if ELspeed==.
drop if TT_dif_hr==.
keep if acct_type=="PRIVATE"&occupancy~="HOV-3"


sort transponder
preserve
keep acct_no
duplicates drop
sort acct_no
outsheet acct_no using ".\data\temp\temp3.txt", replace comma non noq
restore


*g count=1
*egen freq=sum(count), by(acct_no)

*foreach v in TT_dif_hr reliability_dif charged_toll{
*	 sum `v' if freq>5
*	local m`v'=round(`r(mean)',0.01)
*	}
*drop freq count

* II.A.b.1. Read Station List 
file open myfile using ".\data\temp\temp3.txt", read
file read myfile line

while r(eof)==0 { /* while you're not at the end of the file */
	*di "`line'"
	qui {
		preserve
			keep if acct_no=="`line'"
			total MP
			mat b=e(b)
			local A=b[1,1] 
			if `A'>=5{
				reg charged_toll TT_dif_hr reliability_diff  if TT_dif_hr>0, cluster(rt_id)
				matrix   b1=get(_b)
				drop _all
				set obs 1
				gen acct_no="`line'"
				gen urgency=_b[_cons]
				gen urgencyse=_se[_cons]
				gen VOT=_b[TT_dif_hr]
				gen VOTse=_se[TT_dif_hr]
				gen VOR=_b[reliability_diff]
				gen VORse=_se[reliability_diff]
				append using ".\data\temp\temp1"
				save ".\data\temp\temp1", replace
				}
		restore
		}
	display "." _cont
	file read myfile line
	}

file close myfile
use ".\data\temp\temp1", clear
save ".\data\temp\acct_by_acct_noMP5_wvor", replace

drop if urgency==0 | urgency==.
drop if VOT==0 | VOT==.
drop if VOR==0 | VOR==.

drop if urgencyse==0 | urgencyse==.
drop if VOTse==0 | VOTse==.
drop if VORse==0 | VORse==.



sum urgency, detail
local VOU=round(`r(mean)',0.01)
local VOU25=round(`r(p25)',0.01)
local VOU75=round(`r(p75)',0.01)
local N=`r(N)'
sum VOT, detail
local VOT=round(`r(mean)',0.01)
local VOT25=round(`r(p25)',0.01)
local VOT75=round(`r(p75)',0.01)
sum VOR, detail
local VOR=round(`r(mean)',0.01)
local VOR25=round(`r(p25)',0.01)
local VOR75=round(`r(p75)',0.01)





* 95 CI
save ".\data\temp\temp3", replace
clear
set obs 500
gen urgency=0
gen VOT=0
gen VOR=0
save ".\data\temp\CI95_noMP5_wvor", replace emptyok
*relMP rel
foreach j of varlist urgency VOT VOR {
	forvalues i=1(1)500 {
		quietly{
			use ".\data\temp\temp3", clear
			gen temp1=rnormal(`j',`j'se)
			sum temp1
			local h=r(mean)
			use ".\data\temp\CI95_noMP5_wvor", clear
			replace `j'=`h' if _n==`i'
			save ".\data\temp\CI95_noMP5_wvor", replace
			}
			di ".", _continue
		}
	}
sum urgency
local VOUse=round(`r(sd)',0.01)
sum VOT
local VOTse=round(`r(sd)',0.01)
sum VOR
local VORse=round(`r(sd)',0.01)

di "Full Sample"
di "`VOU'"
di "(`VOUse')"
di "[`VOU25',`VOU75']"
di ""
di "`VOT'"
di "(`VOTse')"
di "[`VOT25',`VOT75']"
di ""
di "`VOR'"
di "(`VORse')"
di "[`VOR25',`VOR75']"
di ""
di "Obs. `N'"

quietly{
	local i=4
	local j=`i'+1
	local k=`j'+1
	foreach v in  VOU VOT VOR{
		*local `v'=
		putexcel `c'`i' = ``v''
		putexcel `c'`j' = "(``v'se')"
		putexcel `c'`k' = "[``v'25', ``v'75']"
		local i=`i'+4
		local j=`i'+1
		local k=`j'+1
		}
	}

local cutoff=0.01									// where to truncate reliability (0 or 0.01)
clear

* Get reliability
*I 10 W
use ".\data\clean\HV_ML_reliab", clear
keep if fwy==10&dir=="W"
format date %d
sort date hour
save ".\data\temp\temp2", replace


use ".\data\clean\I10W_laneuse_dataset_15nov14_wcensus", clear

sort date hour
merge m:1 date hour using ".\data\temp\temp2", keep(1 3) nogen 
drop if weekend==1
keep if inlist(hour,5,6,7,8)
*keep if date>td(20oct2013)
drop if holiday==1
drop if transponder==""
drop if transponder=="."
*WTP calculation
gen EL_TT=dist/ELspeed
gen ML_TT=dist/MLspeed
gen MP=inlist(hour,5,6,7,8)&weekend==0
gen TT_dif_hr=ML_TT-EL_TT
gen WTP2=charged_toll/TT_dif_hr



gen reliabilityML=dist/p20_speedML-dist/p50_speedML
gen reliabilityHV=dist/p20_speedHV-dist/p50_speedHV
gen reliability_diff=reliabilityML-reliabilityHV
replace reliability_diff=0 if reliability_diff<`cutoff'

keep if WTP2>0&WTP2~=.
keep if reliability_diff~=.
drop if ELspeed==.
drop if TT_dif_hr==.
keep if acct_type=="PRIVATE"&occupancy~="HOV-3"
preserve
use ".\data\temp\acct_by_acct_noMP5_wvor",clear
drop if urgency==0 | urgency==.
drop if VOT==0 | VOT==.
drop if VOR==0 | VOR==.

drop if urgencyse==0 | urgencyse==.
drop if VOTse==0 | VOTse==.
drop if VORse==0 | VORse==.
keep acct_no
tempfile col3
save `col3'
restore
merge m:1 acct_no using `col3', nogen keep(3)
*local c "D"


foreach v in TT_dif_hr reliability_diff charged_toll{
	qui sum `v' //if e(sample)==1
	local m`v'=round(`r(mean)',.01)
	}
putexcel `c'17="$`mcharged_toll'"
local shr=100*round(`VOU'/`mcharged_toll',.01)
putexcel `c'18="`shr'%"
local ttd=round(`mTT_dif_hr'*60,.01)
local ttd:di %7.2f `ttd'
putexcel `c'19="`ttd'"
local rd=round(`mreliability_diff'*60,.01)
local rd: di %7.2f `rd'
putexcel `c'20="`rd'"
qui unique acct_no //if e(sample)==1
local an=`r(unique)'
putexcel `c'21=`an'


***************************************************************************
// Column III - Post Oct 20  
***************************************************************************
local c "H"
clear
save ".\data\temp\temp1", replace emptyok

use ".\data\clean\I10W_laneuse_dataset_15nov14_wcensus", clear

sort date hour
merge m:1 date hour using ".\data\temp\temp2", keep(1 3) nogen 
drop if weekend==1
keep if inlist(hour,5,6,7,8)
keep if date>td(20oct2013)
drop if holiday==1
drop if transponder==""
drop if transponder=="."
*WTP calculation
gen EL_TT=dist/ELspeed
gen ML_TT=dist/MLspeed
gen MP=inlist(hour,5,6,7,8)&weekend==0
gen TT_dif_hr=ML_TT-EL_TT
gen WTP2=charged_toll/TT_dif_hr



gen reliabilityML=dist/p20_speedML-dist/p50_speedML
gen reliabilityHV=dist/p20_speedHV-dist/p50_speedHV
gen reliability_diff=reliabilityML-reliabilityHV
replace reliability_diff=0 if reliability_diff<`cutoff'

keep if WTP2>0&WTP2~=.
keep if reliability_diff~=.
drop if ELspeed==.
drop if TT_dif_hr==.
keep if acct_type=="PRIVATE"&occupancy~="HOV-3"


sort transponder
preserve
keep acct_no
duplicates drop
sort acct_no
outsheet acct_no using ".\data\temp\temp2.txt", replace comma non noq
restore

/*g count=1
egen freq=sum(count), by(acct_no)

foreach v in TT_dif_hr reliability_dif charged_toll{
	qui sum `v' if freq>5
	local m`v'=round(`r(mean)',0.01)
	}
drop freq count 
*/

* II.A.b.1. Read Station List 
file open myfile using ".\data\temp\temp2.txt", read
file read myfile line

while r(eof)==0 { /* while you're not at the end of the file */
	*di "`line'"
	qui {
		preserve
			keep if acct_no=="`line'"
			total MP
			mat b=e(b)
			local A=b[1,1] 
			if `A'>=5{
				reg charged_toll TT_dif_hr reliability_diff  , cluster(rt_id)
				matrix   b1=get(_b)
				drop _all
				set obs 1
				gen acct_no="`line'"
				gen urgency=_b[_cons]
				gen urgencyse=_se[_cons]
				gen VOT=_b[TT_dif_hr]
				gen VOTse=_se[TT_dif_hr]
				gen VOR=_b[reliability_diff]
				gen VORse=_se[reliability_diff]
				append using ".\data\temp\temp1"
				save ".\data\temp\temp1", replace
				}
		restore
		}
	display "." _cont
	file read myfile line
	}

file close myfile
use ".\data\temp\temp1", clear
save ".\data\temp\acct_by_acct_noMP5_wvor_postoct", replace

drop if urgency==0 | urgency==.
drop if VOT==0 | VOT==.
drop if VOR==0 | VOR==.

drop if urgencyse==0 | urgencyse==.
drop if VOTse==0 | VOTse==.
drop if VORse==0 | VORse==.



sum urgency, detail
local VOU=round(`r(mean)',0.01)
local VOU25=round(`r(p25)',0.01)
local VOU75=round(`r(p75)',0.01)
local N=`r(N)'
sum VOT, detail
local VOT=round(`r(mean)',0.01)
local VOT25=round(`r(p25)',0.01)
local VOT75=round(`r(p75)',0.01)
sum VOR, detail
local VOR=round(`r(mean)',0.01)
local VOR25=round(`r(p25)',0.01)
local VOR75=round(`r(p75)',0.01)


* 95 CI
save ".\data\temp\temp3", replace
clear
set obs 500
gen urgency=0
gen VOT=0
gen VOR=0
save ".\data\temp\CI95_noMP5_wvor_postoct", replace emptyok
*relMP rel
foreach j of varlist urgency VOT VOR {
	forvalues i=1(1)500 {
		quietly{
			use ".\data\temp\temp3", clear
			gen temp1=rnormal(`j',`j'se)
			sum temp1
			local h=r(mean)
			use ".\data\temp\CI95_noMP5_wvor_postoct", clear
			replace `j'=`h' if _n==`i'
			save ".\data\temp\CI95_noMP5_wvor_postoct", replace
			}
			di ".", _cont
		}
	}
sum urgency
local VOUse=round(`r(sd)',0.01)
sum VOT
local VOTse=round(`r(sd)',0.01)
sum VOR
local VORse=round(`r(sd)',0.01)

di "With Reliability"
di "`VOU'"
di "(`VOUse')"
di "[`VOU25',`VOU75']"
di ""
di "`VOT'"
di "(`VOTse')"
di "[`VOT25',`VOT75']"
di ""
di "`VOR'"
di "(`VORse')"
di "[`VOR25',`VOR75']"
di ""
di "Obs. `N'"

quietly{
	local i=4
	local j=`i'+1
	local k=`j'+1
	foreach v in  VOU VOT VOR{
		*local `v'=
		putexcel `c'`i' = ``v''
		putexcel `c'`j' = "(``v'se')"
		putexcel `c'`k' = "[``v'25', ``v'75']"
		local i=`i'+4
		local j=`i'+1
		local k=`j'+1
		}
	
	}

local cutoff=0.01									// where to truncate reliability (0 or 0.01)
clear
* Get reliability
*I 10 W
use ".\data\clean\HV_ML_reliab", clear
keep if fwy==10&dir=="W"
format date %d
sort date hour
save ".\data\temp\temp2", replace


use ".\data\clean\I10W_laneuse_dataset_15nov14_wcensus", clear

sort date hour
merge m:1 date hour using ".\data\temp\temp2", keep(1 3) nogen 
drop if weekend==1
keep if inlist(hour,5,6,7,8)
*keep if date>td(20oct2013)
drop if holiday==1
drop if transponder==""
drop if transponder=="."
*WTP calculation
gen EL_TT=dist/ELspeed
gen ML_TT=dist/MLspeed
gen MP=inlist(hour,5,6,7,8)&weekend==0
gen TT_dif_hr=ML_TT-EL_TT
gen WTP2=charged_toll/TT_dif_hr



gen reliabilityML=dist/p20_speedML-dist/p50_speedML
gen reliabilityHV=dist/p20_speedHV-dist/p50_speedHV
gen reliability_diff=reliabilityML-reliabilityHV
replace reliability_diff=0 if reliability_diff<`cutoff'

keep if WTP2>0&WTP2~=.
keep if reliability_diff~=.
drop if ELspeed==.
drop if TT_dif_hr==.
keep if acct_type=="PRIVATE"&occupancy~="HOV-3"
preserve
use ".\data\temp\acct_by_acct_noMP5_wvor_postoct",clear
drop if urgency==0 | urgency==.
drop if VOT==0 | VOT==.
drop if VOR==0 | VOR==.

drop if urgencyse==0 | urgencyse==.
drop if VOTse==0 | VOTse==.
drop if VORse==0 | VORse==.
keep acct_no
tempfile col3
save `col3'
restore
merge m:1 acct_no using `col3', nogen keep(3)
*local c "D"


foreach v in TT_dif_hr reliability_diff charged_toll{
	qui sum `v' //if e(sample)==1
	local m`v'=round(`r(mean)',.01)
	}
putexcel `c'17="$`mcharged_toll'"
local shr=100*round(`VOU'/`mcharged_toll',.01)
putexcel `c'18="`shr'%"
local ttd=round(`mTT_dif_hr'*60,.01)
local ttd:di %7.2f `ttd'
putexcel `c'19="`ttd'"
local rd=round(`mreliability_diff'*60,.01)
local rd: di %7.2f `rd'
putexcel `c'20="`rd'"
qui unique acct_no //if e(sample)==1
local an=`r(unique)'
putexcel `c'21=`an'

*/
*******************************************************************************
***			 Column IV --- Full Sample, Split Sample IV 				*******
*******************************************************************************
local c "J"

clear
capture file close myfile

save ".\data\temp\temp1", replace emptyok

use ".\data\clean\I10W_laneuse_dataset_15nov14_wcensus", clear

*merge m:1 date hour using "\\rschfs1x\userRS\A-E\arw227_RS\Documents\expresslanes\Spring2015\cleandata\HV_ML_reliab.dta", keep(1 3) nogen 
merge m:1 date hour using ".\data\clean\HV_ML_reliab_30days.dta", keep(1 3) nogen 
*merge m:1 date hour using ".\data\clean\HV_ML_reliab.dta", keep(1 3) nogen   // testing this 5/21 --didn't work

gen reliabilityML=dist/p20_speedML-dist/p50_speedML
gen reliabilityHV=dist/p20_speedHV-dist/p50_speedHV
gen reliability_diff=reliabilityML-reliabilityHV

*WTP calculation
gen TT_dif_hr=dist/MLspeed-dist/ELspeed
gen WTP2=charged_toll/TT_dif_hr
*keep if WTP2>0&WTP2~=.
*keep if WTP2~=.
*keep if WTP2<0
drop if ELspeed==.
drop if TT_dif_hr==.
*drop if weekend==1
drop if holiday==1
la var TT_dif_hr "Time in Hours"
la var reliability_diff "Reliability"
drop if dow==0|dow==6
keep if acct_type=="PRIVATE"&occupancy~="HOV-3"
keep if hour>4 & hour<9
replace reliability_diff=0 if reliability_diff<`cutoff'
g exittime=hh(exit_time)+mm(exit_time)/60+ss(exit_time)/(60*60)
egen exittimemean=mean(exittime), by(acct_no)
g exittimediff=exittime-exittimemean
g TT_dif_hr2 =TT_dif_hr^2
egen ETT_dif_hr=mean(TT_dif_hr),by(dow hour min rt_id)
la var ETT_dif_hr "E[Time in Hours]"
egen id=group(date rt_id)
egen tid=group(date hour rt_id)

preserve
	collapse TT_dif_hr, by(tid date hour rt_id)
	egen t=group(date hour)
	tsset rt_id t
	g L1hr_TTdif=L.TT_dif_hr if date[_n-1]==date[_n]
	g F1hr_TTdif=F.TT_dif_hr if date[_n+1]==date[_n]
	tsset, clear
	drop t
	g dow=dow(date)
	egen t=group(rt_id hour dow)
	g week=week(date)
	tsset t week
	g L3wk_TTdif=L3.TT_dif_hr 
	g L2wk_TTdif=L2.TT_dif_hr 
	g L1wk_TTdif=L.TT_dif_hr 
	g F1wk_TTdif=F.TT_dif_hr 
	g F2wk_TTdif=F2.TT_dif_hr 
	g F3wk_TTdif=F3.TT_dif_hr 
	keep tid  L1hr_TTdif F1hr_TTdif L3wk_TTdif L2wk_TTdif L1wk_TTdif F1wk_TTdif F2wk_TTdif F3wk_TTdif
	tempfile temp
	save `temp'
restore
merge m:1 tid using `temp', keep(3) nogen
sort transponder
preserve
	keep acct_no
	duplicates drop
	sort acct_no
	outsheet acct_no using ".\data\temp\temp3.txt", replace comma non noq
restore

gen MP=inlist(hour,5,6,7,8)&weekend==0


preserve

gen count=1
egen freq=sum(count), by(acct_no)
drop if freq<5
reg TT_dif_hr F1hr_TTdif F1wk_TTdif F2wk_TTdif reliability_diff if TT_dif_hr>0, cluster(rt_id)

g what12= _b[_cons]+F1hr_TTdif*_b[F1hr_TTdif]+F1wk_TTdif*_b[F1wk_TTdif]+F2wk_TTdif*_b[F2wk_TTdif]+reliability_diff*_b[reliability_diff]
g count2=1 if what12!=.
egen freq2=sum(count2), by(acct_no)
drop if freq2<5
drop if TT_dif_hr<=0
g test= charged_toll+reliability_diff +F1hr_TTdif+ F1wk_TTdif+ F2wk_TTdif
drop if test==.
sum charged_toll TT_dif_hr reliability_diff
restore

/* g count=1
egen freq=sum(count), by(acct_no)

foreach v in TT_dif_hr reliability_dif charged_toll{
	qui sum `v' if freq>5
	local m`v'=round(`r(mean)',0.01)
	}
drop freq count
*/
* II.A.b.1. Read Station List 
file open myfile using ".\data\temp\temp3.txt", read
file read myfile line

while r(eof)==0 { /* while you're not at the end of the file */
	*di "`line'"
	qui {
		preserve
			g group=acct_no=="`line'"
			replace group=2 if group==0
			total MP if group==1
			mat b=e(b)
			local A=b[1,1] 
			if `A'>=5{
				reg TT_dif_hr F1hr_TTdif F1wk_TTdif F2wk_TTdif reliability_diff if TT_dif_hr>0 & group==2
				g what12= _b[_cons]+F1hr_TTdif*_b[F1hr_TTdif]+F1wk_TTdif*_b[F1wk_TTdif]+F2wk_TTdif*_b[F2wk_TTdif]+reliability_diff*_b[reliability_diff] if group==1
				sum what12 if group==1 & TT_dif_hr>0 & reliability_diff!=.
				local B=`r(N)'
				if `B'>=5{
					reg charged_toll what12 reliability_diff  if TT_dif_hr>0 & group==1,  cluster(rt_id)
					matrix   b1=get(_b)
					drop _all
					set obs 1
					gen acct_no="`line'"
					gen urgency=_b[_cons]
					gen urgencyse=_se[_cons]
					gen VOT=_b[what12]
					gen VOTse=_se[what12]
					gen VOR=_b[reliability_diff]
					gen VORse=_se[reliability_diff]
					append using ".\data\temp\temp1"
					save ".\data\temp\temp1", replace
					}
				}		
		restore
		}
	display "." _cont
	file read myfile line
	}

file close myfile
use ".\data\temp\temp1", clear
save ".\data\clean\acct_by_acct_splitsampleIV", replace

drop if urgency==0 | urgency==.
drop if VOT==0 | VOT==.
drop if VOR==0 | VOR==.

drop if urgencyse==0 | urgencyse==.
drop if VOTse==0 | VOTse==.
drop if VORse==0 | VORse==.




sum urgency, detail
local VOU=round(`r(mean)',0.01)
local VOU25=round(`r(p25)',0.01)
local VOU75=round(`r(p75)',0.01)
local N=`r(N)'
sum VOT, detail
local VOT=round(`r(mean)',0.01)
local VOT25=round(`r(p25)',0.01)
local VOT75=round(`r(p75)',0.01)
sum VOR, detail
local VOR=round(`r(mean)',0.01)
local VOR25=round(`r(p25)',0.01)
local VOR75=round(`r(p75)',0.01)




* 95 CI
save ".\data\temp\temp3", replace
clear
set obs 500
gen urgency=0
gen VOT=0
gen VOR=0
save ".\data\clean\CI95_splitsampleIV", replace emptyok
*relMP rel
foreach j of varlist urgency VOT VOR{
	forvalues i=1(1)500 {
		quietly{
			use ".\data\temp\temp3", clear
			gen temp1=rnormal(`j',`j'se)
			sum temp1
			local h=r(mean)
			use ".\data\clean\CI95_splitsampleIV", clear
			replace `j'=`h' if _n==`i'
			save ".\data\clean\CI95_splitsampleIV", replace
			}
			di ".", _continue
		}
	}
sum urgency
local VOUse=round(`r(sd)',0.01)
sum VOT
local VOTse=round(`r(sd)',0.01)
sum VOR
local VORse=round(`r(sd)',0.01)
		


quietly{
	local i=4
	local j=`i'+1
	local k=`j'+1
	foreach v in  VOU VOT VOR{
		*local `v'=
		putexcel `c'`i' = ``v''
		putexcel `c'`j' = "(``v'se')"
		putexcel `c'`k' = "[``v'25', ``v'75']"
		local i=`i'+4
		local j=`i'+1
		local k=`j'+1
		}
	
	}

local cutoff=0.01									// where to truncate reliability (0 or 0.01)
clear

* Get reliability
*I 10 W
use ".\data\clean\HV_ML_reliab", clear
keep if fwy==10&dir=="W"
format date %d
sort date hour
save ".\data\temp\temp2", replace


use ".\data\clean\I10W_laneuse_dataset_15nov14_wcensus", clear

sort date hour
merge m:1 date hour using ".\data\temp\temp2", keep(1 3) nogen 
drop if weekend==1
keep if inlist(hour,5,6,7,8)
*keep if date>td(20oct2013)
drop if holiday==1
drop if transponder==""
drop if transponder=="."
*WTP calculation
gen EL_TT=dist/ELspeed
gen ML_TT=dist/MLspeed
gen MP=inlist(hour,5,6,7,8)&weekend==0
gen TT_dif_hr=ML_TT-EL_TT
gen WTP2=charged_toll/TT_dif_hr



gen reliabilityML=dist/p20_speedML-dist/p50_speedML
gen reliabilityHV=dist/p20_speedHV-dist/p50_speedHV
gen reliability_diff=reliabilityML-reliabilityHV
replace reliability_diff=0 if reliability_diff<`cutoff'

keep if WTP2>0&WTP2~=.
keep if reliability_diff~=.
drop if ELspeed==.
drop if TT_dif_hr==.
keep if acct_type=="PRIVATE"&occupancy~="HOV-3"
preserve
use ".\data\clean\acct_by_acct_splitsampleIV",clear
drop if urgency==0 | urgency==.
drop if VOT==0 | VOT==.
drop if VOR==0 | VOR==.

drop if urgencyse==0 | urgencyse==.
drop if VOTse==0 | VOTse==.
drop if VORse==0 | VORse==.
keep acct_no
tempfile col3
save `col3'
restore
merge m:1 acct_no using `col3', nogen keep(3)
*local c "D"


foreach v in TT_dif_hr reliability_diff charged_toll{
	qui sum `v' //if e(sample)==1
	local m`v'=round(`r(mean)',.01)
	}
putexcel `c'17="$`mcharged_toll'"
local shr=100*round(`VOU'/`mcharged_toll',.01)
putexcel `c'18="`shr'%"
local ttd=round(`mTT_dif_hr'*60,.01)
local ttd:di %7.2f `ttd'
putexcel `c'19="`ttd'"
local rd=round(`mreliability_diff'*60,.01)
local rd: di %7.2f `rd'
putexcel `c'20="`rd'"
qui unique acct_no //if e(sample)==1
local an=`r(unique)'
putexcel `c'21=`an'

capture log close
